#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<vector<int>> res;
    void dfs(int level, vector<int>& storage, map<int, int> tree)
    {
        if (level == storage.size())
        {
            res.push_back(storage);
            return;
        }
        for (auto& pair : tree)
        {
            if (pair.second > 0)
            {
                --pair.second;
                storage.push_back(pair.first);
                dfs(level, storage, tree);
                storage.pop_back();
                ++pair.second;
            }
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<int> tmp;
        map<int, int> tree;
        for (auto& e : nums) ++tree[e];
        dfs(nums.size(), tmp, tree);
        return res;
    }
};